﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using _3DTicTacToe.Core;

namespace _3DTicTacToe
{
    internal partial class GameWindow : Window
    {
        private Point _mLastPos;
        private bool _mDown;
        private TicTacToeGame _game;
        private bool _showStartWindow = true;

        private bool _turnWindowShown;
        private TurnListWindow _turnWindow;

        void Init(TicTacToeGame game)
        {
            _game = game;

            // init 3d viewport
            model.Transform = new Transform3DGroup();

            // assign event handlers
            _game.OnTurn += _game_OnTurn;
            _game.OnTurn += _game_OnTurn_1;
            _game.OnGameOver += _game_OnGameOver;

            // prepare player panel
            _game_OnTurn(null);
        }

        void _game_OnGameOver(object sender, GameOverEventArgs e)
        {
            if (e.IsDraw)
            {
                playerBorder.Background = Brushes.Black;
                playerControl.Foreground = Brushes.White;
                playerControl.Content = "Draw! You're all Losers!";
            }
            else
            {
                playerBorder.Background = new SolidColorBrush(e.WinningPlayer.Color);
                playerControl.Foreground = new SolidColorBrush(InvertColor(e.WinningPlayer.Color));
                playerControl.Content = e.WinningPlayer.Name + " wins!";
            }

            _showStartWindow = false;
            menuBar.IsEnabled = false;

            if (_turnWindowShown)
                _turnWindow.Close();

            Closing -= Window_Closing;

            var gameOverWin = new GameOverWindow(e, _game) {Owner = this, Topmost = true};
            gameOverWin.Closing += GameOverWinClosing;
            gameOverWin.Show();
        }

        void GameOverWinClosing(object sender, CancelEventArgs e)
        {
            Close();
        }

        public GameWindow()
        {
            InitializeComponent();
        }

        public GameWindow(TicTacToeGame game)
            :this()
        {
            Init(game);
        }

        private void Window_Closing(object sender, EventArgs e)
        {
            if (_turnWindowShown)
                _turnWindow.Close();

            if (_showStartWindow)
                new StartWindow().Show();
        }

        // exit
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        // return to start: close
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            Close();
        }

        // new game
        private void MenuItem_Click_NewGame(object sender, RoutedEventArgs e)
        {
            _showStartWindow = false;
            new NewGameDialog().Show();
            Close();
        }

        // restart
        private void MenuItem_Click_Restart(object sender, RoutedEventArgs e)
        {
            var game = new TicTacToeGame(_game.PlayerOne, _game.PlayerTwo);
            _showStartWindow = false;
            
            new GameWindow(game).Show();
            Close();
        }

        void HandleTurnListWindowClosing(object sender, CancelEventArgs e)
        {
            _game.OnTurn -= _turnWindow.TurnHandler;
            turnListMenuItem.IsChecked = false;
            _turnWindowShown = false;
        }

        private void turnListMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if  (_turnWindowShown)
            {
                _game.OnTurn -= _turnWindow.TurnHandler;
                _turnWindow.Close();
            }
            else
            {
                _turnWindow = new TurnListWindow(_game.Turns);
                _game.OnTurn += _turnWindow.TurnHandler;
                _turnWindow.Closing += HandleTurnListWindowClosing;
                _turnWindow.Show();
                Focus();

                _turnWindowShown = true;
            }
        }
    }
}
